/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mx.itsc.dawi.view;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import mx.its.dawi.BL.usuariosBLLocal;
import mx.its.dawi.dao.UsuariosDaoLocal;
import mx.its.dawi.model.Usuarios;
import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.StreamedContent;
import org.primefaces.model.UploadedFile;

/**
 *
 * @author Martinez
 */
@ManagedBean(name = "UserBean")
@SessionScoped
public class UserBean {

    @EJB
    private usuariosBLLocal usuariosBL;
    @EJB
    private UsuariosDaoLocal usuariosDao;
    private Usuarios user;
    private UploadedFile file;
    private List<Usuarios> userList;
    private StreamedContent fotoStreamed;

    /**
     * Creates a new instance of UserBean
     */
    public UserBean() {
        user = new Usuarios();
    }

    public List<Usuarios> getUserList() {
        userList = usuariosDao.findAll();
        return userList;
    }

    public void setUserList(List<Usuarios> userList) {
        this.userList = userList;
    }

    public Usuarios getUser() {
        return user;
    }

    public void setUser(Usuarios user) {
        this.user = user;
    }

    public UploadedFile getFile() {
        return file;
    }

    public void setFile(UploadedFile file) {
        this.file = file;
    }

    public String newUser() {
        //System.out.println("paso 1");
        user.setId(0);
        usuariosBL.registrarUsuario(user);
        //System.out.println("paso 2");
        return "UserList";
    }

    public void obtenerNameFile() {
        user.setFoto(file.getContents());
        //System.out.println("name-------------> "+file.getFileName());
    }
//     public void handleFileUpload(FileUploadEvent event) {  
//        FacesMessage msg = new FacesMessage("Succesful", event.getFile().getFileName() + " is uploaded.");  
//        FacesContext.getCurrentInstance().addMessage(null, msg);  
//    }  

    public String prepareNewUser (){
        user = new Usuarios();
        return "UserList";
    }
    
    public StreamedContent getFotoStreamed() {
        try {
            BufferedOutputStream bfo = null;
            File f = new File("file.jpg");
            FileOutputStream fo = new FileOutputStream(f);
            bfo = new BufferedOutputStream(fo);
            bfo.write(user.getFoto());
            bfo.close();
            InputStream inp = new FileInputStream(f);
            
            fotoStreamed = new DefaultStreamedContent( inp, "image/jpeg");
            
        } catch (Exception ex) {
            Logger.getLogger(UserBean.class.getName()).log(Level.SEVERE, null, ex);
        }
        return fotoStreamed;
    }

    public void setFotoStreamed(StreamedContent fotoStreamed) {
        this.fotoStreamed = fotoStreamed;
    }
}
